package org.lcc.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.lcc.mapper.ExaminationmanagementMapper;
import org.lcc.model.entity.Exam;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class ExamStatusUpdateJob implements Job {

    @Autowired
    private ExaminationmanagementMapper examinationmanagementMapper;
    // 定义ANSI颜色常量
    private static final String BLUE = "\033[94m";  // 亮蓝色
    private static final String RESET = "\033[0m"; // 重置颜色
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("考试状态更新任务已触发，当前时间：{}", LocalDateTime.now());

        LocalDateTime now = LocalDateTime.now();
        List<String> excludeStatus = Arrays.asList("待发布","待添加试题","已结束");
        List<Exam> exams = examinationmanagementMapper.selectExamsExcludeStatus(excludeStatus);

        for (Exam exam : exams) {
            LocalDateTime startTime = exam.getStartTime();//  考试开始时间
            int duration = exam.getDuration();// 考试时长
            String currentStatus = exam.getStatus();
            String newStatus = calculateStatus(now, startTime, duration);
            boolean b = newStatus != null;
            // 修改日志输出
            log.info("{}考试ID: {} 状态: {}{}", BLUE, exam.getExamId(), currentStatus, RESET);
            boolean b1 = !newStatus.equals(currentStatus);
            log.info("{}考试ID: {} 新的状态: {}{}", BLUE, exam.getExamId(), newStatus, RESET);
            if (b&&b1) {
                log.info("{}开始更新考试状态...{}", BLUE, RESET);
                exam.setStatus(newStatus);
                examinationmanagementMapper.updateById(exam);
                log.info("{}考试ID: {} 状态更新为: {}{}", BLUE, exam.getExamId(), newStatus, RESET);
            }
        }
    }


    private String calculateStatus(LocalDateTime now, LocalDateTime startTime, int duration) {
        log.info("当前时间：{}", BLUE, now,RESET);
        LocalDateTime examEndTime = startTime.plusMinutes(duration); // 计算考试结束时间
        LocalDateTime fifteenMinutesAfterStart = startTime.plusMinutes(15); // 计算开始时间 15 分钟后的时刻

        // 判断当前时间是否在考试结束时间之后（包括等于结束时间）
        if (now.isAfter(examEndTime) || now.equals(examEndTime)) {
            log.info("当前时间1：{}", BLUE, 1,RESET);
            return "已结束";
        }
        // 判断当前时间是否在开始时间 15 分钟之后，但未到考试结束时间
        else if (now.isAfter(fifteenMinutesAfterStart)) {
            log.info("当前时间2：{}", BLUE, 2,RESET);
            return "超过规定时间，不可进入考试";
        }
        // 判断当前时间是否在考试开始时间和考试开始时间 15 分钟之间（包括等于开始时间和 15 分钟后的时刻）
        else if ((now.isAfter(startTime) || now.equals(startTime)) && (now.isBefore(fifteenMinutesAfterStart) || now.equals(fifteenMinutesAfterStart))) {
            log.info("当前时间3：{}", BLUE, 3,RESET);
            return "进行中";
        }
        // 当前时间在考试开始时间 15 分钟之内
        else {
            log.info("当前时间4：{}", BLUE, 4,RESET);
            return "待开始";
        }
    }
}